package commonControllers
import (
	"net"
	"fmt"
	//"encoding/json"
	//."LiquidMeasurement/lib"

)


var (
   socket *net.UDPConn

)

type UdpServerController struct {

}

func SetupUdpServer()  {
	var err error
	socket, err = net.ListenUDP("udp4", &net.UDPAddr{
		IP:   net.IPv4(0, 0, 0, 0),
		Port: 10087,
	})
	if err != nil {
		fmt.Println("监听失败!", err)
		return
	}
	defer socket.Close()

	for {
		// 读取数据
		data := make([]byte, 4096)
		read, remoteAddr, err := socket.ReadFromUDP(data)
		if err != nil {
			fmt.Println("读取数据失败!", err)
			break
		}
		fmt.Println(read, remoteAddr)
		fmt.Printf("%s\n\n", data)
		//err = json.Unmarshal(data, &TcpUdpCmd)
		if  err != nil{

		}
		// 发送数据
//		senddata := []byte("hello client!")
//		_, err = socket.WriteToUDP(senddata, remoteAddr)
//		if err != nil {
//			return
//			fmt.Println("发送数据失败!", err)
//		}
	}
}

